package view;

import controller.*;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JOptionPane;
import model.information.Event;
import model.information.Contact;

/**
 *
 * @author tobias
 */
public class EventPanel extends javax.swing.JPanel {
    private Controller ctrl;
    private ArrayList<Event> events;
    private Event currentEvent;

    /** Creates new form EventPanel */
    public EventPanel(Controller ctrl) {
        initComponents();
        this.ctrl = ctrl;
        this.events = ctrl.getEvents();
        updateList();
    }

    public void updateList() {
        eventList.setListData(events.toArray());
    }

    public void updateFromSave(Object o) {
        eventList.repaint();
    }

    public void addParticipant(Contact c) {
        currentEvent.addParticipant(c);
        participantsList.setListData(currentEvent.getParticipants().toArray());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        eventListPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        eventList = new javax.swing.JList();
        delBtn = new javax.swing.JButton();
        addBtn = new javax.swing.JButton();
        currentEventPanel = new javax.swing.JPanel();
        saveBtn = new javax.swing.JButton();
        titleField = new javax.swing.JTextField();
        locationField = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        noteArea = new javax.swing.JTextArea();
        sHourField = new javax.swing.JComboBox();
        sMinuteField = new javax.swing.JComboBox();
        sMonthField = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        sDateField = new javax.swing.JComboBox();
        sYearField = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        eHourField = new javax.swing.JComboBox();
        eMinuteField = new javax.swing.JComboBox();
        eDateField = new javax.swing.JComboBox();
        eMonthField = new javax.swing.JComboBox();
        eYearField = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        participantsList = new javax.swing.JList();
        jLabel9 = new javax.swing.JLabel();
        addParticipantBtn = new javax.swing.JButton();
        remParticipantBtn = new javax.swing.JButton();

        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(816, 586));

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+3));
        jLabel1.setText("Aftaler");

        eventListPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Kommende aftaler:"));
        eventListPanel.setOpaque(false);
        eventListPanel.setPreferredSize(new java.awt.Dimension(230, 518));

        eventList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        eventList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                eventListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(eventList);

        delBtn.setText("Slet aftale");
        delBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delBtnActionPerformed(evt);
            }
        });

        addBtn.setText("Opret ny aftale");
        addBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout eventListPanelLayout = new org.jdesktop.layout.GroupLayout(eventListPanel);
        eventListPanel.setLayout(eventListPanelLayout);
        eventListPanelLayout.setHorizontalGroup(
            eventListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, eventListPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(eventListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, delBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, addBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE))
                .addContainerGap())
        );
        eventListPanelLayout.setVerticalGroup(
            eventListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, eventListPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(delBtn)
                .addContainerGap())
        );

        currentEventPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Aftale:"));
        currentEventPanel.setOpaque(false);
        currentEventPanel.setPreferredSize(new java.awt.Dimension(532, 518));

        saveBtn.setText("Gem ændringer");
        saveBtn.setEnabled(false);
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        titleField.setEnabled(false);

        locationField.setEnabled(false);

        noteArea.setColumns(20);
        noteArea.setLineWrap(true);
        noteArea.setRows(5);
        noteArea.setWrapStyleWord(true);
        noteArea.setEnabled(false);
        jScrollPane2.setViewportView(noteArea);

        sHourField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }));
        sHourField.setEnabled(false);

        sMinuteField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55" }));
        sMinuteField.setEnabled(false);

        sMonthField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Januar", "Februar", "Marts", "April", "Maj", "Juni", "Juli", "August", "September", "Oktober", "Noveber", "December" }));
        sMonthField.setEnabled(false);

        jLabel6.setText("Note");

        jLabel4.setText("Start");

        jLabel3.setText("Sted");

        jLabel2.setText("Titel");

        sDateField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1.", "2.", "3.", "4.", "5.", "6.", "7.", "8.", "9.", "10.", "11.", "12.", "13.", "14.", "15.", "16.", "17", "18.", "19.", "20.", "21.", "22.", "23.", "24.", "25.", "26.", "27.", "28.", "29.", "30.", "31." }));
        sDateField.setEnabled(false);

        sYearField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" }));
        sYearField.setEnabled(false);

        jLabel5.setText("Slut");

        eHourField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }));
        eHourField.setEnabled(false);

        eMinuteField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55" }));
        eMinuteField.setEnabled(false);

        eDateField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1.", "2.", "3.", "4.", "5.", "6.", "7.", "8.", "9.", "10.", "11.", "12.", "13.", "14.", "15.", "16.", "17", "18.", "19.", "20.", "21.", "22.", "23.", "24.", "25.", "26.", "27.", "28.", "29.", "30.", "31." }));
        eDateField.setEnabled(false);

        eMonthField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Januar", "Februar", "Marts", "April", "Maj", "Juni", "Juli", "August", "September", "Oktober", "Noveber", "December" }));
        eMonthField.setEnabled(false);

        eYearField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" }));
        eYearField.setEnabled(false);

        jLabel7.setText("kl.:");

        jLabel8.setText("kl.:");

        participantsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        participantsList.setEnabled(false);
        jScrollPane3.setViewportView(participantsList);

        jLabel9.setText("Deltagere");

        addParticipantBtn.setText("Tilføj ny deltager");
        addParticipantBtn.setEnabled(false);
        addParticipantBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addParticipantBtnActionPerformed(evt);
            }
        });

        remParticipantBtn.setText("Fjern deltager");
        remParticipantBtn.setEnabled(false);
        remParticipantBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                remParticipantBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout currentEventPanelLayout = new org.jdesktop.layout.GroupLayout(currentEventPanel);
        currentEventPanel.setLayout(currentEventPanelLayout);
        currentEventPanelLayout.setHorizontalGroup(
            currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, currentEventPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(saveBtn)
                    .add(currentEventPanelLayout.createSequentialGroup()
                        .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel4)
                            .add(jLabel2)
                            .add(jLabel3)
                            .add(jLabel5))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, currentEventPanelLayout.createSequentialGroup()
                                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel6)
                                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel9)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, remParticipantBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, addParticipantBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .add(org.jdesktop.layout.GroupLayout.LEADING, currentEventPanelLayout.createSequentialGroup()
                                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(sDateField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(eDateField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(currentEventPanelLayout.createSequentialGroup()
                                        .add(sMonthField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 107, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(sYearField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(currentEventPanelLayout.createSequentialGroup()
                                        .add(eMonthField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 107, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(eYearField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                .add(7, 7, 7)
                                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(jLabel8)
                                    .add(jLabel7))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(sHourField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 73, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(eHourField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 73, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(sMinuteField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(eMinuteField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(titleField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE)
                            .add(locationField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE))))
                .addContainerGap())
        );
        currentEventPanelLayout.setVerticalGroup(
            currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(currentEventPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(titleField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(locationField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(currentEventPanelLayout.createSequentialGroup()
                            .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(sMonthField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(sYearField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(eMonthField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(eYearField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(currentEventPanelLayout.createSequentialGroup()
                            .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(sDateField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jLabel4))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(eDateField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jLabel5))))
                    .add(currentEventPanelLayout.createSequentialGroup()
                        .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(sHourField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel7)
                            .add(sMinuteField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(eHourField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel8)
                            .add(eMinuteField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(currentEventPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(currentEventPanelLayout.createSequentialGroup()
                        .add(jLabel6)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE))
                    .add(currentEventPanelLayout.createSequentialGroup()
                        .add(jLabel9)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addParticipantBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(remParticipantBtn)))
                .add(32, 32, 32)
                .add(saveBtn)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(layout.createSequentialGroup()
                        .add(eventListPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(currentEventPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jLabel1))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, currentEventPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, eventListPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE))
                .addContainerGap(20, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBtnActionPerformed
        Event newEvent = ctrl.addEvent("Ny aftale", "", "");
        eventList.setSelectedValue(newEvent, true);
        titleField.requestFocus();
}//GEN-LAST:event_addBtnActionPerformed

    private void eventListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_eventListValueChanged
        if(eventList.getSelectedIndex() != -1) {
            currentEvent = (Event)eventList.getSelectedValue();
            titleField.setText(currentEvent.getTitle());
            titleField.setEnabled(true);
            locationField.setText(currentEvent.getLocation());
            locationField.setEnabled(true);

            Calendar sTime = currentEvent.getStartTime();
            sHourField.setSelectedIndex(sTime.get(Calendar.HOUR_OF_DAY));
            sHourField.setEnabled(true);
            int sMinute = (int)(sTime.get(Calendar.MINUTE) * 0.2);
            sMinuteField.setSelectedIndex(sMinute);
            sMinuteField.setEnabled(true);
            sDateField.setSelectedIndex(sTime.get(Calendar.DATE) - 1);
            sDateField.setEnabled(true);
            sMonthField.setSelectedIndex(sTime.get(Calendar.MONTH));
            sMonthField.setEnabled(true);
            sYearField.setSelectedIndex(sTime.get(Calendar.YEAR) - 2009);
            sYearField.setEnabled(true);

            Calendar eTime = currentEvent.getEndTime();
            eHourField.setSelectedIndex(eTime.get(Calendar.HOUR_OF_DAY));
            eHourField.setEnabled(true);
            int eMinute = (int)(eTime.get(Calendar.MINUTE) * 0.2);
            eMinuteField.setSelectedIndex(eMinute);
            eMinuteField.setEnabled(true);
            eDateField.setSelectedIndex(eTime.get(Calendar.DATE) - 1);
            eDateField.setEnabled(true);
            eMonthField.setSelectedIndex(eTime.get(Calendar.MONTH));
            eMonthField.setEnabled(true);
            eYearField.setSelectedIndex(eTime.get(Calendar.YEAR) - 2009);
            eYearField.setEnabled(true);

            noteArea.setText(currentEvent.getNote());
            noteArea.setEnabled(true);
            participantsList.setListData(currentEvent.getParticipants().toArray());
            participantsList.setEnabled(true);
            saveBtn.setEnabled(true);
            addParticipantBtn.setEnabled(true);
            remParticipantBtn.setEnabled(true);
        } else {
            titleField.setText("");
            titleField.setEnabled(false);
            locationField.setText("");
            locationField.setEnabled(false);
            sHourField.setSelectedIndex(0);
            sHourField.setEnabled(false);
            sMinuteField.setSelectedIndex(0);
            sMinuteField.setEnabled(false);
            sDateField.setSelectedIndex(0);
            sDateField.setEnabled(false);
            sMonthField.setSelectedIndex(0);
            sMonthField.setEnabled(false);
            sYearField.setSelectedIndex(0);
            sYearField.setEnabled(false);
            eHourField.setSelectedIndex(0);
            eHourField.setEnabled(false);
            eMinuteField.setSelectedIndex(0);
            eMinuteField.setEnabled(false);
            eDateField.setSelectedIndex(0);
            eDateField.setEnabled(false);
            eMonthField.setSelectedIndex(0);
            eMonthField.setEnabled(false);
            eYearField.setSelectedIndex(0);
            eYearField.setEnabled(false);
            noteArea.setText("");
            noteArea.setEnabled(false);
            participantsList.setListData(new Object[0]);
            participantsList.setEnabled(false);
            saveBtn.setEnabled(false);
            addParticipantBtn.setEnabled(false);
            remParticipantBtn.setEnabled(false);
        }
    }//GEN-LAST:event_eventListValueChanged

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        if(titleField.getText().equals("")) {
            JOptionPane.showMessageDialog(ctrl.getFrame(), "Et eller flere felter er ikke udfyldt korrekt, udfyld venligst en titel, og prøv igen.", "Mangelde oplysninger", JOptionPane.WARNING_MESSAGE);
        } else {
            currentEvent.setTitle(titleField.getText());
            currentEvent.setLocation(locationField.getText());

            int sHour = sHourField.getSelectedIndex();
            int sMinute = sMinuteField.getSelectedIndex() * 5;
            int sDate = sDateField.getSelectedIndex() + 1;
            int sMonth = sMonthField.getSelectedIndex() + 1;
            int sYear = sYearField.getSelectedIndex() + 2009;
            currentEvent.setStartTime(sYear, sMonth, sDate, sHour, sMinute);

            int eHour = eHourField.getSelectedIndex();
            int eMinute = eMinuteField.getSelectedIndex() * 5;
            int eDate = eDateField.getSelectedIndex() + 1;
            int eMonth = eMonthField.getSelectedIndex() + 1;
            int eYear = eYearField.getSelectedIndex() + 2009;
            currentEvent.setEndTime(eYear, eMonth, eDate, eHour, eMinute);

            currentEvent.setNote(noteArea.getText());
        }
    }//GEN-LAST:event_saveBtnActionPerformed

    private void delBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delBtnActionPerformed
        if(eventList.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(ctrl.getFrame(), "Der er ikke valgt en aftale der skal slettes, vælg venligst en aftale, og prøv igen.", "Vælg aftale", JOptionPane.WARNING_MESSAGE);
        } else {
            ctrl.removeEvent(currentEvent);
        }
    }//GEN-LAST:event_delBtnActionPerformed

    private void remParticipantBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remParticipantBtnActionPerformed
        if(participantsList.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(ctrl.getFrame(), "Der er ikke valgt en deltager der skal fjernes fra aftalen, vælg venligst en deltager, og prøv igen.", "Vælg deltager", JOptionPane.WARNING_MESSAGE);
        } else {
            currentEvent.removeParticipant((Contact)participantsList.getSelectedValue());
            participantsList.setListData(currentEvent.getParticipants().toArray());
        }
    }//GEN-LAST:event_remParticipantBtnActionPerformed

    private void addParticipantBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addParticipantBtnActionPerformed
        ParticipantDialog pDialog = new ParticipantDialog(ctrl.getFrame(), true, this, ctrl);
        pDialog.setVisible(true);
    }//GEN-LAST:event_addParticipantBtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBtn;
    private javax.swing.JButton addParticipantBtn;
    private javax.swing.JPanel currentEventPanel;
    private javax.swing.JButton delBtn;
    private javax.swing.JComboBox eDateField;
    private javax.swing.JComboBox eHourField;
    private javax.swing.JComboBox eMinuteField;
    private javax.swing.JComboBox eMonthField;
    private javax.swing.JComboBox eYearField;
    private javax.swing.JList eventList;
    private javax.swing.JPanel eventListPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField locationField;
    private javax.swing.JTextArea noteArea;
    private javax.swing.JList participantsList;
    private javax.swing.JButton remParticipantBtn;
    private javax.swing.JComboBox sDateField;
    private javax.swing.JComboBox sHourField;
    private javax.swing.JComboBox sMinuteField;
    private javax.swing.JComboBox sMonthField;
    private javax.swing.JComboBox sYearField;
    private javax.swing.JButton saveBtn;
    private javax.swing.JTextField titleField;
    // End of variables declaration//GEN-END:variables

}
